﻿using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace DeviceManager.ContentPage
{
    /// <summary>
    /// HomePage.xaml 的交互逻辑
    /// </summary>
    public partial class HomePage : Page
    {
        private System.Timers.Timer tmr = null;

        private const int RefDataTick = 60;
        private int RefIndex = 0;

        public HomePage()
        {
            InitializeComponent();
            this.Loaded += HomePage_Loaded;
        }

        private void HomePage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                RefData();
                tmr = new System.Timers.Timer();
                tmr.Interval = 1000 * RefDataTick;
                tmr.Elapsed += Tmr_Elapsed;
                tmr.Start();
            }
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new ThreadStart(delegate ()
            {
                RefData();

            }), null);
        }

        internal void RefData()
        {
            int offLineCount = ManagerDBCtrl.GetMng().GetOffLineDeviceCount();
            int maintainCount = ManagerDBCtrl.GetMng().GetMaintainDeviceCount();
            int warringCount = ManagerDBCtrl.GetMng().GetWarrDeviceCount();
            int deviceCount = ManagerDBCtrl.GetMng().GetDeviceCount();
            //离线设备，即设备已离线
            tb_offLineDeviceCount.Text = offLineCount.ToString();
            //维护设备数
            tb_MaintainCount.Text = maintainCount.ToString();
            //告警设备数
            tb_waringDeviceCount.Text = warringCount.ToString();
            //正常设备数
            tb_noneCount.Text = (deviceCount - offLineCount - maintainCount - warringCount).ToString();

            //读取统计表数据
            //读取新增。移除数
            LoadAddDelChartDatas();
            ///告警信息
            LoadAddWaringChartDatas();

            //维护和故障设备
            LoadErrorDevices();
            //预测
            LoadForecastDevices();

        }

        /// <summary>
        /// 加载新增，移除统计图表
        /// </summary>
        private void LoadAddDelChartDatas()
        {
            Title title = new Visifire.Charts.Title();
            title.Text = "近7天新增、移除数";
            chart_1.Titles.Clear();
            chart_1.Titles.Add(title);
            chart_1.Series.Clear();
            chart_1.Series.Add(GetDataSeriesByType(SumDataType.SDT_ADD, "新增数"));
            chart_1.Series.Add(GetDataSeriesByType(SumDataType.SDT_DEL, "移除数"));

        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private DataSeries GetDataSeriesByType(SumDataType type,string name,bool isForecast=false)
        {
            DataSeries ds = new DataSeries();
            ds.RenderAs = RenderAs.Spline;
            ds.LabelStyle = LabelStyles.Inside;
            ds.LabelEnabled = true;
            ds.LegendText = name;
            List<StatisticsDataNode> data1 = ManagerDBCtrl.GetMng().GetStatisticsDateNodes(type);
            DateTime maxDatetime = DateTime.MinValue;
            int minValue = int.MaxValue;
            int maxValue = int.MinValue;
            foreach (var de in data1)
            {
                ds.DataPoints.Add(new DataPoint() { AxisXLabel = de.SumDate, YValue = de.Value });
                if (maxDatetime < de.sumDate)
                {
                    maxDatetime = de.sumDate;
                }
                if (minValue > de.Value)
                {
                    minValue = de.Value;
                }
                if(maxValue < de.Value)
                {
                    maxValue = de.Value;
                }
            }
            if (isForecast)
            {
                Random rd = new Random((int)DateTime.Now.Ticks % int.MaxValue);
                ds.DataPoints.Add(new DataPoint() { AxisXLabel = maxDatetime.AddDays(1).ToString("yyyy-MM-dd"), YValue = rd.Next(minValue, maxValue) });
            }
            return ds;
        }

        /// <summary>
        /// 加载告警和故障统计图表
        /// </summary>
        private void LoadAddWaringChartDatas()
        {
            Title title = new Visifire.Charts.Title();
            title.Text = "近7天告警信息";
            chart_2.Titles.Clear();
            chart_2.Titles.Add(title);
            chart_2.Series.Clear();
            chart_2.Series.Add(GetDataSeriesByType(SumDataType.SDT_CURRENT, "电流异常"));
            chart_2.Series.Add(GetDataSeriesByType(SumDataType.SDT_HUMIDITY, "湿度异常"));
            chart_2.Series.Add(GetDataSeriesByType(SumDataType.SDT_TEMPERATURE, "温度异常"));
            chart_2.Series.Add(GetDataSeriesByType(SumDataType.SDT_VOLTAGE, "电压异常"));
        }

        /// <summary>
        /// 加载离线和维护设备信息
        /// </summary>
        private void LoadErrorDevices()
        {
            Title title = new Visifire.Charts.Title();
            title.Text = "近7天离线和维护设备信息";
            chart_3.Titles.Clear();
            chart_3.Titles.Add(title);
            chart_3.Series.Clear();
            chart_3.Series.Add(GetDataSeriesByType(SumDataType.SDT_OFFLINE, "离线设备"));
            chart_3.Series.Add(GetDataSeriesByType(SumDataType.SDT_MAINTAIN, "维护设备"));
        }


        private void LoadForecastDevices()
        {
            Title title = new Visifire.Charts.Title();
            title.Text = "设备预测数据";
            chart_4.Titles.Clear();
            chart_4.Titles.Add(title);
            chart_4.Series.Clear();
            chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_OFFLINE, "离线设备", true));
            chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_MAINTAIN, "维护设备", true));
            //chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_CURRENT, "电流异常", true));
            //chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_HUMIDITY, "湿度异常", true));
            //chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_TEMPERATURE, "温度异常", true));
            //chart_4.Series.Add(GetDataSeriesByType(SumDataType.SDT_VOLTAGE, "电压异常",true));
        }


        //去水印
        void chart_Rendered(object sender, EventArgs e)
        {
            var c = sender as Chart;
            var legend = c.Legends[0];
            var root = legend.Parent as Grid;
            //直接将其移除掉
            root.Children.RemoveAt(10);
            root.Children.RemoveAt(8);
        }
    }


   
}
